package cn.star.jots.film.service.impl;

import cn.star.jots.film.mapper.FilmMapper;
import cn.star.jots.film.pojo.dto.FilmQuery;
import cn.star.jots.film.pojo.dto.FilmSaveParam;
import cn.star.jots.film.pojo.dto.FilmSubjectQuery;
import cn.star.jots.film.pojo.dto.UserFilmQuery;
import cn.star.jots.film.pojo.entity.Film;
import cn.star.jots.film.pojo.vo.FilmDataVO;
import cn.star.jots.film.pojo.vo.FilmVO;
import cn.star.jots.film.service.FilmService;
import cn.star.jots.filmActor.mapper.FAMapper;
import cn.star.jots.preview.mapper.PreviewMapper;
import cn.star.jots.tag.mapper.FilmTagMapper;
import cn.star.jots.tag.pojo.entity.FilmTag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class FilmServiceImpl implements FilmService {
    @Autowired
    FilmMapper filmMapper;
    @Autowired
    FilmTagMapper filmTagMapper;
    @Autowired
    FAMapper faMapper;
    @Autowired
    PreviewMapper previewMapper;

    @Override
    public List<FilmVO> selectAll(FilmQuery filmQuery) {
        log.debug("查询电影,参数:{}", filmQuery);
        return filmMapper.selectAll(filmQuery);
    }

    @Override
    public List<FilmVO> selectSubjectFilm(FilmSubjectQuery filmSubjectQuery) {
        log.debug("查询专题的电影信息,参数:{}", filmSubjectQuery);
        return filmMapper.selectSubjectFilm(filmSubjectQuery);
    }

    @Override
    @Transactional
    public Long save(FilmSaveParam filmSaveParam) {
        log.debug("保存电影信息,参数:{}", filmSaveParam);
        Film film = new Film();
        BeanUtils.copyProperties(filmSaveParam, film);
        Date releaseDate = filmSaveParam.getReleaseDate();
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
        String releaseYear = yearFormat.format(releaseDate);
        film.setReleaseYear(releaseYear);
        film.setStatus("1");
        film.setCreateTime(new Date());
        filmMapper.insert(film);

        List<String> tags = filmSaveParam.getTag();
        for (String tag : tags) {
            FilmTag filmTag = new FilmTag();
            filmTag.setFilmId(film.getId());
            filmTag.setTag(tag);
            filmTag.setCreateTime(new Date());
            filmTagMapper.insert(filmTag);
        }
        Long id = film.getId();
        return id;
    }

    @Transactional
    @Override
    public void deleteFilm(Long id) {
        log.debug("删除电影,参数:{}", id);
        filmMapper.deleteFilm(id);
        filmTagMapper.deleteByFilmId(id);
        faMapper.deleteByFilmId(id);
        previewMapper.deleteByFilmId(id);
    }

    @Override
    public void update(FilmSaveParam filmSaveParam) {
        log.debug("更新电影信息,参数:{}", filmSaveParam);
        Film film = new Film();
        BeanUtils.copyProperties(filmSaveParam, film);
        film.setUpdateTime(new Date());
        filmMapper.update(film);
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("更新电影状态:id={},status={}", id, status);
        Film film = new Film();
        film.setId(id);
        film.setStatus(status);
        film.setUpdateTime(new Date());
        filmMapper.update(film);
    }

    @Override
    public List<FilmVO> selectUserFilm(UserFilmQuery userFilmQuery) {
        log.debug("查询用户电影信息:userFilmQuery={}", userFilmQuery);
        return filmMapper.selectUserFilm(userFilmQuery);
    }

    @Override
    public FilmDataVO selectData() {
        log.debug("查询电影数据");
        return filmMapper.selectData();
    }
}
